var fs = require('fs')
var rp = require('request-promise')
var cheerio = require('cheerio')

var config = require('./config.js')
var util = require('./util/_default.js')
var xlsx = require('node-xlsx')

var proxyUrl = '', trycount = 10
var page, maxpage


var helper = util.helper

var dt = [['序号', '药品识别码', '药品名称', '药品剂型', '药品规格', '包装单位',
    '包装材质', '转换系数', '批准文号', '药品类别', '生产企业', '中标企业', '中标年份', '中标价格（元）', '状态']]

var href = 'http://202.100.92.51:8888/bdcp/hos/bid-hos-show!list.do'

function main(page) {

    console.log('正在抓取第' + page + '页,当前总数：' + dt.length)
    var opts = helper.deepCopy(config.reqOpts)
    opts.url = href + '?pageNo=' + page
    opts.method = 'Post'
    opts.headers.Referer = href

    rp(opts).then(function (body) {
        var $ = cheerio.load(body)
        var tr = $('tr', 'table.tablelist')
        if (tr.length > 0) {
            tr.slice(1).each(function () {
                var row = []
                row.push(
                    iGetInnerText($("td", this).eq(0).text()),
                    iGetInnerText($("td", this).eq(1).text()),
                    iGetInnerText($("td", this).eq(2).text()),
                    iGetInnerText($("td", this).eq(3).text()),
                    iGetInnerText($("td", this).eq(4).text()),
                    iGetInnerText($("td", this).eq(5).text()),
                    iGetInnerText($("td", this).eq(6).text()),
                    iGetInnerText($("td", this).eq(7).text()),
                    iGetInnerText($("td", this).eq(8).text()),
                    iGetInnerText($("td", this).eq(9).text()),
                    iGetInnerText($("td", this).eq(10).text()),
                    iGetInnerText($("td", this).eq(11).text()),
                    iGetInnerText($("td", this).eq(12).text()),
                    iGetInnerText($("td", this).eq(13).attr('title')),
                    iGetInnerText($("td", this).eq(14).text())
                )
                dt.push(row)
            });
            if (page % 300 == 0) {
                saveExcel(dt, page)
            }
            if (page <= 2490) {
                page = page + 1
                main(page)
            } else {
                saveExcel(dt, page)
            }
        }
    }).catch(function (err) {
        console.log(err)
        setTimeout(function () {
            main(page)
        }, Math.random() * 10 * 1000)
    })
}

function iGetInnerText(testStr) {
    var resultStr = testStr.replace(/\ +/g, ""); //去掉空格
    resultStr = testStr.replace(/[ ]/g, "");    //去掉空格
    resultStr = testStr.replace(/[\t\r\n]/g, ""); //去掉回车换行
    resultStr = testStr.replace(/[ ]/g, "");    //去掉空格
    return resultStr;
}

function saveExcel(data, page) {
    d = (new Date()).Format('hhmmss')
    var file = xlsx.build([{ name: "mySheetName", data: data }])
    fs.writeFileSync('result_' + page + '_' + d + '.xlsx', file, 'binary')
}


main(1)



